var securityModule = angular.module('Security', []);


securityModule.factory('$Security', ['$http',function() {
	
function redirect(url) {
    url = url || '/';
    $location.path(url);
  }	
	
	var securityManager;
	securityManager = {
		currentUser: null,
		role: null,
		isAuthenticated: function() {
				return !!this.currentUser;			
			},
		isCoordinator: function() {
				return !!(securityManager.currentUser && securityManager.role == 'coordinator');			
			},
		login: function(name, passwd) {
				/*var request = $http.post('/login', {username: name, passwd: passwd});
				return request.success(function(response)) {
						service.currentUser = response.data.username;
						service.role = response.data.role;					
					}*/
				securityManager.currentUser = name;
				securityManager.role = 'coordinator';
				return securityManager.isAuthenticated();
			},
		logout: function(redirectTo) {
				$http.post('/logout').success(function() {
					securityManager.currentUser = null;
					redirect(redirectTo);
					});			
			},
		isParticipant: function() {
			return !!(securityManager.role == 'participant');
			},
		getCurrentUser: function() {
			return currentUser;
			}			
		}
		return securityManager;		
	}]);